﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Swan.WakeUpLibrary.Library;

namespace Swan.SwanWakeUp.AlarmActions
{
    public class YrWeatherAction
    {
        string url;

        public YrWeatherAction()
        { 
            // http://www.yr.no/place/Sweden/Stockholm/Stockholm/forecast.xml
            this.url = Properties.Settings.Default.YrURL + "/forecast.xml";
        }

        /// <summary>
        /// The function that returns the current conditions for the specified location.
        /// </summary>
        /// <param name="location">City or ZIP code</param>
        /// <returns></returns>
        public Conditions GetCurrentConditions(string url)
        {
            Conditions conditions = new Conditions();

            XmlDocument xmlConditions = new XmlDocument();
            xmlConditions.Load(this.url);

            conditions.City = xmlConditions.SelectSingleNode("/weatherdata/location/name").InnerText;
            conditions.Condition = xmlConditions.SelectSingleNode("/weatherdata/forecast/tabular/time/symbol").Attributes["name"].InnerText;
            conditions.TempC = xmlConditions.SelectSingleNode("/weatherdata/forecast/tabular/time/temperature").Attributes["value"].InnerText;
            //conditions.TempF = xmlConditions.SelectSingleNode("/xml_api_reply/weather/current_conditions/temp_f").Attributes["data"].InnerText;
            //conditions.Humidity = xmlConditions.SelectSingleNode("/xml_api_reply/weather/current_conditions/humidity").Attributes["data"].InnerText;
            conditions.Wind = xmlConditions.SelectSingleNode("/weatherdata/forecast/tabular/time/windSpeed").Attributes["name"].InnerText;
            conditions.WindDir = xmlConditions.SelectSingleNode("/weatherdata/forecast/tabular/time/windDirection").Attributes["name"].InnerText;
            return conditions;
        }
    }
}
